package com.jms.boot.context;

import com.jms.framework.config.JmsConfig;
import com.jms.framework.context.AbstractApplicationContext;
import com.jms.framework.exception.PropertyAlreadySetException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * JmsBootApplicationContext 类扩展了 AbstractApplicationContext，
 * 用于初始化和管理 JMS Boot 应用程序的上下文。
 * 它接收一个 Configuration 对象作为配置源，并在构造时进行初始化。
 *
 * @author jms
 * @since 2025-01-07
 */
public class JmsBootApplicationContext extends AbstractApplicationContext {

    private static final Logger logger = LogManager.getLogger(JmsBootApplicationContext.class);
    private volatile static JmsBootApplicationContext applicationContext;


    /**
     * 私有构造函数，用于创建 JmsBootApplicationContext 的唯一实例。
     */
    private JmsBootApplicationContext() {
    }

    /**
     * 设置 JmsConfig 对象，用于初始化 JmsBootApplicationContext。
     * @param jmsConfig jms应用配置对象
     */
    public void setJmsConfig(JmsConfig jmsConfig) {
        if (jmsConfig == null) {
            throw new PropertyAlreadySetException(JmsConfig.class, "jmsConfig");
        }
        this.jmsConfig = jmsConfig;
    }

    public static JmsBootApplicationContext getInstance() {
        if (applicationContext == null) {
            synchronized (JmsBootApplicationContext.class) {
                if (applicationContext == null) {
                    applicationContext = new JmsBootApplicationContext();
                }
            }
        }
        return applicationContext;
    }

    @Override
    public void init() {
        logger.info("JmsBoot application context starts initializing.");
        super.init();
        logger.info("JmsBoot application context initialization is complete.");
    }

    @Override
    public void destroy() {
        logger.info("JmsBoot application context starts destroying.");
        super.destroy();
        logger.info("JmsBoot application context destruction is complete.");
    }
}
